package chen.web.test.blog;

import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import chen.web.blog.Blog;
import chen.web.blog.BlogManager;
import chen.web.blog.Category;
import chen.web.blog.Category.Position;
import chen.web.blog.CategoryManager;
import chen.web.blog.PositionException;
import chen.web.test.TestUtils;
import chen.web.user.Account;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/test.xml"})
public class CategoryManagerTesting {

	private @Autowired ApplicationContext context;
	private @Autowired BlogManager bm;
	private @Autowired CategoryManager cm;
	
	private TestUtils tu;
	private BlogTestUtils btu;
	
	Account testAcc;
	Blog testBlog;
	
	@Before
	public void setUp(){
		tu = new TestUtils(context);
		btu = new BlogTestUtils(context);
		testAcc = tu.setupAccount();
		tu.setCurrentAccount(testAcc);
		testBlog = btu.setupBlog(testAcc);
	}
	@After
	public void teardown(){
		tu.teardownAccount(testAcc);
		btu.teardownBlog(testBlog);
	}
	
	@Test
	public void testCategoryGRUD(){
		//add root category
		Category cat = new Category(testBlog, new Position(null, null));
		cat.setName("hello");
		cat.setDescription("hi");;
		cat = cm.add(cat);
		Assert.assertNotNull(cat);
		Assert.assertTrue(cat.getId() > 0);
		//add a child
		Category child = new Category(testBlog, new Position(cat, null));
		child.setName("hello");
		child.setDescription("hi ");
		child = cm.add(child);
		//get root categories
		List<Category> roots = cm.getRootCategories(testBlog);
		Assert.assertTrue(roots.size() == 1);
		Assert.assertTrue(roots.get(0).getChildren().size() == 1);
		//update
		child.setName("hiii");
		child.setDescription("hii");
		cm.update(child);
		//delete
		cm.delete(child);
		cm.delete(cat);
		roots = cm.getRootCategories(testBlog);
		cat = cm.get(testBlog, cat.getId());
		Assert.assertNull(cat);
	}
	
	@Test
	public void testMove(){
		Category cat = null;
		Category child = null;
		try{
			//add root category
			cat = new Category(testBlog, new Position(null, null));
			cat.setName("hello");
			cat.setDescription("hi");
			cat = cm.add(cat);
			Assert.assertNotNull(cat);
			Assert.assertTrue(cat.getId() > 0);
			//add a child
			child = new Category(testBlog, new Position(cat, null));
			child.setName("hello");
			child.setDescription("hi ");
			child = cm.add(child);
			//重新加载数据
			cat = cm.get(testBlog, cat.getId());
			child = cm.get(testBlog, child.getId());
			try{
				//移动到下级栏目，预期异常
				cm.move(cat, new Position(child, null));	
				Assert.fail();
			}catch(PositionException ex){
				
			}
			
			cm.move(child, new Position(null,null));
			//重新加载
			child = cm.get(testBlog, child.getId());
			Assert.assertNull(child.getPosition().getParent());
		}finally{
			//删除测试数据
			cm.delete(child);
			cm.delete(cat);	
		}
	}

}
